package JDBC;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LoginDemo {
    public static void main(String[] args) {
        while (true) {
            UserInfo userInfo = InputUtil.getInputObject(new UserInfo(), "欢迎登录", "登录");

            try (Connection connection = DBUtil.getConnection()) {
                Statement statement = connection.createStatement();
                String sql = "SELECT username,password,nickname,age " +
                        "FROM userinfo " +
                        "WHERE username='" + userInfo.getUsername() + "' " +
                        "AND password = '" + userInfo.getPassword() + "';";
                ResultSet rs = statement.executeQuery(sql);
                if (rs.next()) {
                    String username = rs.getString("username");
                    String password = rs.getString("password");
                    String nickname = rs.getString("nickname");
                    int age = rs.getInt("age");
                    System.out.println(username + ":" + password + ":" + nickname + ":" + age);
                    break;
                } else {
                    System.out.println("用户名或密码不正确");
                }

            } catch (SQLException e) {
                e.printStackTrace();

            }
        }
    }
}